﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;

namespace ZSens
{   
    class Serializing
    {
        public static int hashByteArray(byte[] data, int start, int end)
        {
            unchecked
            {
                const int p = 16777619;
                int hash = (int)2166136261;

                for (int i = start; i < end; i++)
                    hash = (hash ^ data[i]) * p;

                hash += hash << 13;
                hash ^= hash >> 7;
                hash += hash << 3;
                hash ^= hash >> 17;
                hash += hash << 5;
                return hash;
            }
        }
        public static int writeIntoByteArray(byte[] dat, int idx, byte x)
        {
            if (idx < dat.Length)
            {
                dat[idx] = x;
                return idx + 1;
            }
            return -1;
        }

        public static int writeIntoByteArray(byte[] dat, int idx, Int32 x)
        {
            if (idx + sizeof(Int32) > dat.Length)
                return -1;
            dat[idx] = (byte) ((x >> 24) & 0xff);
            dat[idx + 1] = (byte)((x >> 16) & 0xff);
            dat[idx + 2] = (byte)((x >> 8) & 0xff);
            dat[idx + 3] = (byte)(x & 0xff);
            return idx + sizeof(Int32);
        }

        public static int writeIntoByteArray(byte[] dat, int idx, String s)
        {
            idx = writeIntoByteArray(dat, idx, (Int32)s.Length);
            if (idx < 0 || idx + s.Length > dat.Length)
                return -1;
            for (int i = 0; i < s.Length; i++)
                dat[idx + i] = (byte) s[i];
            return idx + s.Length;
        }

        public static unsafe int writeIntoByteArray(byte[] dat, int idx, float f)
        {
            Int32 t = *((Int32*)&f);
            return writeIntoByteArray(dat, idx, t);
        }


        public static int readFromByteArray(byte[] dat, int idx, ref byte x)
        {
            if (idx < dat.Length)
            {
                x = dat[idx];
                return idx + 1;
            }
            return -1;
        }

        public static int readFromByteArray(byte[] dat, int idx, ref Int32 x)
        {
            if (idx + 3 < dat.Length)
            {
                x = (Int32)((dat[idx] << 24) + (dat[idx + 1] << 16) + (dat[idx + 2] << 8) + (dat[idx + 3]));
                return idx + 4;
            }
            return -1;
        }

        public static unsafe int readFromByteArray(byte[] dat, int idx, ref float x)
        {
            if (idx + 3 < dat.Length)
            {
                int t = (Int32)((dat[idx] << 24) | (dat[idx + 1] << 16) | (dat[idx + 2] << 8) | (dat[idx + 3]));
                x = *((float*) &t);
                return idx + 4;
            }    
            return -1;
        }

        public static int readFromByteArray(byte[] dat, int idx, ref String s)
        {
            Int32 length = 0;
            if ((idx = readFromByteArray(dat, idx, ref length)) < 0)
                return -1;
            if (idx + length > dat.Length) 
                return -1;
            s = System.Text.Encoding.UTF8.GetString(dat, idx, length);
            return idx + length;
        }
    }
}
